/*
 * Licensed this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */
package org.boapi.shop;

/**
 * International Standard Serial Number is a 8 digits number for periodic papers like journals. 
 * @author Sebastian Ritter
 * @version 3.0.0.0
 */
public class ISSN implements org.boapi.core.IChecknumber {

    /**
     *  Creates a ISSN Object from GTIN
     *  @param gtin
     *  @return ISSN
     */
    public static ISSN parse (final GTIN gtin) {
      if (!gtin.getNumber ().startsWith("977")) {
        throw new IllegalArgumentException ("GTIN is not an ISSN, because it starts not with 977");
      }
      // GTIN = 977072486790p => 0724-8679
      final ISSN instance = new ISSN ();
      String value = gtin.getNumber ().toString ().substring (3, 11);
      instance.setCompleteValue (new String[]{value});
      instance.isChecknumberCorrect ();
      value = value.substring(0, value.length() - 2) + instance.getChecknumber ();
      instance.setCompleteValue (new String[] {value});
      return instance;
    }
    /// <summary>
    /// International Standard Serial Number
    /// </summary>
    private transient String number;
    /// <summary>
    /// Set the ISSN
    /// </summary>
    /// <param name="issn"></param>
    public void setCompleteValue (final String[] issn) {
      final StringBuilder nummer = new StringBuilder ();
      for (Character c : issn[0].toUpperCase().toCharArray ()) {
        if (Character.isDigit (c) || c == 'X') {
          nummer.append (c);
        }
        else if (c != '-') {
          throw new IllegalArgumentException ("Illegal character in ISSN");
        }
      }
      while (nummer.length() < 8) {
        nummer.insert (0,'0');
      }
      number = nummer.toString ();
      this.checknumber = 'R';
    }
    
    @Override
    public void setCompleteValue (String issn) {
    	this.setCompleteValue(new String[]{issn});
    }

    /**
     *  The number of ISSN
     *  @return String number
     */
    public String getNumber () {
      return this.number;
    }

    /// <summary>
    /// The valid checknumber
    /// </summary>
    private char checknumber = 'R';
    /// <summary>
    /// Return the valid checknumber
    /// </summary>
    /// <returns></returns>
    private char getChecknumber () {
      if (this.checknumber == 'R') {
        this.isChecknumberCorrect ();
      }
      return this.checknumber;
    }

  @Override
    public boolean isChecknumberCorrect () {
      int[] factors = new int[] { 8, 7, 6, 5, 4, 3, 2 }; // only first seven
      int[] digits = new int[factors.length]; 
      for (int i = 0; i < digits.length; i++) {
        digits[i] = Integer.parseInt(this.number.substring (i,i+ 1));
      }
      int sum = 0;
      for (int i = 0; i < digits.length; i++) {
        sum += digits[i] * factors[i];
      }
      int mod11 = sum % 11;
      int checkDigit = 11 - mod11;
      switch (checkDigit) {
        case 10: 
          this.checknumber = 'X';
          break;
        case 11:
          this.checknumber = '0';
          break;
        default:
          this.checknumber = (""+checkDigit).charAt(0);
          break;
      }
      return this.checknumber == this.number.toCharArray()[7];
    }
  }


